/**
 * https://leetcode.cn/submissions/detail/361487125
 * 209. 长度最小的子数组
 * medium, 黄驰 2022.9.11
 * 滑动窗口
 **/

#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int sum = 0, len = nums.size() + 1;
        for (int start = 0, end = 0; end < nums.size(); ++end) {
            sum += nums[end];
            while (sum >= target) {
                len = min(len, end - start + 1);
                if (len == 1) return len;
                sum -= nums[start++];
            }
        }
        return len > nums.size() ? 0 : len;
    }
};